/**
 * Copyright 2021 xpstem.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xpstem.lelink.operation;

import com.xpstem.lelink.type.Data16;
import com.xpstem.lelink.type.Data32;
import com.xpstem.lelink.type.Data8;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Global variable encoding.
 * @author billy zhang
 */
public class GVEncoder {

    /**
     * short format
     */
    private static final short GV_SHORT_PREFIX =    0b0110_0000;
    /**
     * long format, 1 byte follow
     */
    private static final short GV_BYTE1_PREFIX =    0b1110_0001;
    /**
     * long format, 2 bytes follow
     */
    private static final short GV_BYTE2_PREFIX =    0b1110_0010;
    /**
     * long format, 4 bytes follow
     */
    private static final short GV_BYTE4_PREFIX =    0b1110_0011;
    /**
     * zero terminated string
     */
    private static final short GV_STR_PREFIX =      0b1110_0100;

    private static final int BYTE0_MAX = 32;
    private static final int BYTE1_MAX = 128;
    private static final int BYTE2_MAX = 32768;
    private int intValue;

    private String strValue;

    private GVEncoder(int value) {
        this.intValue = value;
    }

    private GVEncoder(String value) {
        this.strValue = value;
    }

    public int getIntValue() {
        return intValue;
    }

    public String getStrValue() { return strValue; }

    public static GVEncoder from(int value) {
        return new GVEncoder(value);
    }

    public static GVEncoder from(String value) {
        return new GVEncoder(value);
    }

    public static GVEncoder from(Data8 value) {
        return new GVEncoder(value.getValue());
    }

    public static GVEncoder from(Data16 value) {
        return new GVEncoder(value.getValue());
    }

    public static GVEncoder from(Data32 value) {
        return new GVEncoder(value.getValue());
    }

    public void gv0(OutputStream outStream) throws IOException {
        int encode = GV_SHORT_PREFIX + (intValue & 0b0001_1111);
        outStream.write(encode);
    }

    private void gv1(OutputStream outStream) throws IOException {
        outStream.write(GV_BYTE1_PREFIX);
        outStream.write((byte) intValue);
    }

    private void gv2(OutputStream outStream) throws IOException {
        outStream.write(GV_BYTE2_PREFIX);
        outStream.write((byte) intValue);
        outStream.write((byte)(intValue >>8));
    }

    private void gv4(OutputStream outStream) throws IOException {
        outStream.write(GV_BYTE4_PREFIX);
        outStream.write((byte) intValue);
        outStream.write((byte)(intValue >>8));
        outStream.write((byte)(intValue >>16));
        outStream.write((byte)(intValue >>24));
    }

    public void gvx(OutputStream outStream) throws IOException {
        if (intValue < BYTE0_MAX) {
            gv0(outStream);
            return;
        }

        if (intValue < BYTE1_MAX) {
            gv1(outStream);
            return;
        }

        if (intValue < BYTE2_MAX) {
            gv2(outStream);
            return;
        }

        gv4(outStream);
    }
}
